<template>
    <cl-crud ref="Crud">
        <cl-view-head v-if="route.query.id" />
        <cl-row>
            <!-- 刷新按钮 -->
            <cl-refresh-btn />
            <!-- 删除按钮 -->
            <cl-multi-delete-btn />
            <!-- 状态 -->
            <cl-filter label="播放状态">
                <cl-select :options="options.status" prop="status" :width="200" />
            </cl-filter>
            <!-- 状态 -->
            <cl-filter label="创建时间">
                <cl-date-picker type="datetimerange" v-model="datePicker" />
            </cl-filter>
            <cl-flex1 />
            <!-- 关键字搜索 -->
            <cl-search-key />
        </cl-row>

        <cl-row>
            <!-- 数据表格 -->
            <cl-table ref="Table" />
        </cl-row>

        <cl-row>
            <cl-flex1 />
            <!-- 分页控件 -->
            <cl-pagination />
        </cl-row>

        <!-- 新增、编辑 -->
        <cl-upsert ref="Upsert" />
    </cl-crud>
</template>

<script lang="ts" name="user-info" setup>
import { useCrud, useTable, useUpsert } from '@cool-vue/crud';
import { reactive, ref } from 'vue';
import { useCool } from '/@/cool';
import dayjs from 'dayjs';
const { service, route } = useCool();
const datePicker = ref([
	dayjs().format('YYYY-MM-DD 00:00:00'),
	dayjs().format('YYYY-MM-DD 23:59:59')
]);
// 数据选项
const options = reactive({
	status: [
		{
			label: '播放中途退出',
			value: 0,
			type: 'warning'
		},
		{
			label: '正常播放结束',
			value: 1,
			type: 'success'
		},
		{
			label: '广告加载失败',
			value: 2,
			type: 'danger'
		}
	]
});
// cl-upsert
const Upsert = useUpsert({
	items: [
		{
			label: '用户ID',
			prop: 'userId',
			hook: 'number',
			component: { name: 'el-input-number' },
			required: true
		},
		{
			label: '播放状态',
			prop: 'status',
			component: {
				name: 'el-radio-group',
				options: [
					{ label: '播放中途退出', value: 0 },
					{ label: '正常播放结束', value: 1 }
				]
			},
			value: 0,
			required: true
		}
	]
});

// cl-table
const Table = useTable({
	columns: [
		{ type: 'selection' },
		{ label: '用户ID', prop: 'userId' },
		{
			label: '昵称',
			prop: 'nickName',
			minWidth: 120
		},
		{
			label: '头像',
			prop: 'avatarUrl',
			minWidth: 100,
			component: {
				name: 'cl-avatar'
			}
		},
		{
			label: '播放状态',
			prop: 'status',
			minWidth: 100,
			dict: options.status
		},
		{
			label: '备注',
			prop: 'remark',
			minWidth: 200,
			showOverflowTooltip: true
		},
		{
			label: '更新时间',
			prop: 'updateTime',
			component: { name: 'cl-date-text' }
		}
		// { type: 'op', buttons: ['delete'] }
	]
});

// cl-crud
const Crud = useCrud(
	{
		service: service.user.adInfo
	},
	app => {
		app.refresh({
			startTime: dayjs().format('YYYY-MM-DD 00:00:00'),
			endTime: dayjs().format('YYYY-MM-DD 23:59:59'),
			userId: route.query.id
		});
	}
);
</script>